<?php

namespace backend\models\financial;

use Yii;
use yii\data\Pagination;
/**
 * This is the model class for table "edu_virtual".
 *
 * @property integer $virtual_id
 * @property integer $user_id
 * @property string $username
 * @property integer $virtual_num
 * @property string $vartual_detail
 * @property integer $virtual_addtime
 * @property integer $virtual_status
 * @property string $virtual_serial
 */
class Virtual extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_virtual';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'virtual_num', 'virtual_addtime', 'virtual_status'], 'integer'],
            [['username'], 'string', 'max' => 50],
            [['vartual_detail'], 'string', 'max' => 100],
            [['virtual_serial'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'virtual_id' => 'Virtual ID',
            'user_id' => 'User ID',
            'username' => 'Username',
            'virtual_num' => 'Virtual Num',
            'vartual_detail' => 'Vartual Detail',
            'virtual_addtime' => 'Virtual Addtime',
            'virtual_status' => 'Virtual Status',
            'virtual_serial' => 'Virtual Serial',
        ];
    }
    
    /**
     * 积分列表 查询数据库生成列表
     */
    public function getVirtualList() {
        $where_data = $this->whereInfo(Yii::$app->request->post());
       
        if(isset($where_data['virtual_addtime'])){
            $time = $where_data['virtual_addtime']?$where_data['virtual_addtime']:$session->get('virtual_addtime');
            unset($where_data['virtual_addtime']);           
            $query = Virtual::find()->where($where_data)->andwhere($time);
        }else{
            unset($where_data['virtual_addtime']);
            $query = Virtual::find()->where($where_data);
        }        
        
        $countQuery = clone $query;
		$pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => '6']);
		$models = $query->offset($pages->offset)
		->orderby('virtual_addtime desc')
        ->limit($pages->limit)
        ->asArray()
        ->all();
		foreach ($models as $key=>$val){
		    $models[$key]['vartual_info'] = $this->cn_substr_utf8($val['vartual_detail'],30);
		}
		return ['pages' => $pages, 'lists' => $models];
    }
    
    /**
     * 积分列表 数据查询条件拼接
     */
    public function whereInfo($data = array()){
        $session = Yii::$app->session;
        $session->open();
       
        if(Yii::$app->request->get('del')==1){
            $session->remove('virtual_status');
            $session->remove('virtual_addtime');
            $session->remove('keyword');
            $session->remove('keywordname');
            $session->remove('virtual_addtime');
            $session->has('virtual_serial') ? $session->remove('virtual_serial') : $session->remove('username');
        }
        if(isset($data['type'])){
		$data['type']=$data['type'];
		}else{
		
		 $data['type']=$session->get('virtual_status');
		}
       // $data['type'] = $data['type']?$data['type']:$session->get('virtual_status');
        if ((!empty($data['type']))&&($data['type']!=0)) {
            $where_data['virtual_status'] = $data['type'];
            $session->set('virtual_status',$where_data['virtual_status']);
        }
        
       
       
        //$data['createdtime'] = $data['createdtime']?$data['createdtime']:$session->get('virtual_addtime');
        if(isset($data['createdtime'])){
		$data['createdtime']=$data['createdtime'];
		}else{
		$data['createdtime']=$session->get('virtual_addtime');
		}
        
           
            if ((!empty($data['createdtime']))&&($data['createdtime']!=0)) {            
                $where_data['virtual_addtime'] = ['>','virtual_addtime',$data['createdtime']];
                
                $session->set('virtual_addtime',$data['createdtime']);
               
            }
        
        
        $username = $session->has('virtual_serial')?$session->get('virtual_serial'):$session->get('username');
        //$data['username'] = $data['username']?$data['username']:$username;  

		if(isset($data['username'])){
		$data['username']=$data['username'];
		}else{
		$data['username']=$username;
		}
        
        if (!empty($data['username'])&&$data['keywordtype']!='notl') { 
                if($data['keywordtype']=='virtual_serial'){
                    $session->remove('username');
                }else if($data['keywordtype']=='username'){
                    $session->remove('virtual_serial');
                }
                $keywordtype = $session->has('virtual_serial')?'virtual_serial':'username';
                $data['keywordtype'] = $data['keywordtype']?$data['keywordtype']:$keywordtype;
                $where_data[$data['keywordtype']] = $data['username'];
                $session->set($data['keywordtype'],$data['username']);
                $session->set('keyword',$data['keywordtype']);
                $session->set('keywordname',$data['username']);
        }   
		if(!isset($where_data)){
		
		$where_data=array();
		}
        return $where_data;        
    }
    
    /**
     * 积分数据 导出
     */
    public function getInfo() {
        $where_data = $this->whereInfo();         
        if(isset($where_data['virtual_addtime'])){
            $time = $where_data['virtual_addtime']?$where_data['virtual_addtime']:$session->get('virtual_addtime');
            unset($where_data['virtual_addtime']);
            $arr = $this->find()->where($where_data)->andwhere($time)->asArray()->all();
        }else if(count($where_data)>=1){
            unset($where_data['virtual_addtime']);
            $arr = $this->find()->where($where_data)->asArray()->all();
        }else{
            $arr = $this->find()->asArray()->all();
        }
        return $arr;
    }
    
   /**
    * 字符串精确截取
    */ 
    private function cn_substr_utf8($str,$length,$append='...',$start=0){
        if(strlen($str)<$start+1){
            return '';
        }
        preg_match_all("/./su",$str,$ar);
        $str2='';
        $tstr='';
        //www.phpernote.com
        for($i=0;isset($ar[0][$i]);$i++){
            if(strlen($tstr)<$start){
                $tstr.=$ar[0][$i];
            }else{
                if(strlen($str2)<$length + strlen($ar[0][$i])){
                    $str2.=$ar[0][$i];
                }else{
                    break;
                }
            }
        }
        return $str==$str2?$str2:$str2.$append;
    }
    
}
